<?php

namespace App\Http\Controllers\Api;


use App\Http\Requests\Api\VerificationCodeRequest;
use Illuminate\Http\Request;
use \Illuminate\Support\Facades\Cache;

class VerificationCodesController extends Controller
{
    //
    public function store(VerificationCodeRequest $verificationCodeRequest)
    {
        $phone = $verificationCodeRequest->phone;

        $code = str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);
        $key = str_pad(random_int(1, 9999), 8, 0, STR_PAD_LEFT);

        $expiredAt = now()->addMinutes(10);

        // 缓存验证码 10分钟过期。
        Cache::put($key, ['phone' => $phone, 'code' => $code], $expiredAt);
        return $this->response->array(['key'=>$key,'code' => $code])->setStatusCode(201);
    }
}
